<?php

class AskpageController extends GTL_Action {

    public function init() {
        parent::init();
        /* Initialize action controller here */
    }

    public function indexAction() {

        $askData = Array();
        $userData = Array();
        $askForm = new Form_Ask();

        $errorMessage = Array();
        $successmessage = '';

        //Load Question Model
        $questionModel = new Model_Question();

        //Load User Model
        $userModel = new Model_User();

        $q_id = null;
        $q_insert_id = null;
        $u_id = null;
        $url_lgn_id = '';
        $url_id = '';
        $statusHash = '';
        $url_hashtag = '';
        $question = '';

        $urlModel = new Model_Url();

        //Check for the url id Parameter
        $hashtag = $this->_request->getParam('hashtag');

        if (trim($hashtag) == '') {
            $statusHash = 0;
        } else {
            $hashData = $urlModel->fetchEntryByHashTag($hashtag);


            if ($hashData) {
                $statusHash = $hashData['url_status'];
                $url_lgn_id = $hashData['url_lgn_id'];
                $url_id = $hashData['url_id'];
                $url_hashtag = $hashData['url_hashtag'];
                
            }
        }


        if ($this->_request->isPost()) {
            $postedData = $this->_request->getPost();
            if (!$askForm->isValid($postedData)) {
                $errorMessage = $askForm->getMessages();
            } else {
                $u_id = '0'; //Default user id is 0 so no user generated
                if ($postedData['email'] != '' || $postedData['phone'] != '') {
                    //Save User 
                    $userData['u_id'] = NULL;
                    $userData['u_name'] = ""; //$postedData['name'];
                    $userData['u_phone'] = $postedData['phone'];
                    $userData['u_email'] = $postedData['email'];
                    $userData['u_status'] = 1;
                    $u_id = $userModel->save($userData, 'u_id');
                }

                //Save question
                $askData['q_id'] = $q_id;
                $askData['q_u_id'] = $u_id;
                $askData['q_lgn_id'] = $postedData['url_lgn_id'];
                $askData['q_url_id'] = $postedData['url_id'];
                $askData['q_question'] = $postedData['question'];
                $askData['q_answer'] = '';
                $askData['q_createddate'] = date("Y-m-d H:i:s");
                $askData['q_answerddate'] = date("Y-m-d H:i:s");
                $askData['q_status'] = 1;
                
                $question = $postedData['question'];

                $q_insert_id = $questionModel->save($askData, 'q_id');
                $askForm->reset();
                $successmessage = "Thanks for participating";
            }
        }
        
        
        $this->view->q_u_id = $u_id;
        $this->view->q_question = $question;
        $this->view->url_hashtag = $url_hashtag;
        $this->view->q_insert_id = $q_insert_id;
        $this->view->url_lgn_id = $url_lgn_id;
        $this->view->url_id = $url_id;
        $this->view->hashtag = $hashtag;
        $this->view->statusHash = $statusHash;
        $this->view->form = $askForm;
        $this->view->successmessage = $successmessage;
        $this->view->errorMessage = $errorMessage;
    }

}

?>
